package com.lhy.admin.common.mvcplus.wrapper.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lhy.admin.common.mvcplus.entity.ComparatorEnum;
import com.lhy.admin.common.mvcplus.entity.DbField;

/**
 * JSONObjectArrayNotContainsComparator
 *
 * @author hyluan
 * @date 2022年11月21日 14:11
 */
public class JSONObjectArrayNotContainsComparator extends JSONObjectArrayContainsComparator {

    public static final String JSON_NOT_CONTAINS_SQL_TEMP = "JSON_CONTAINS(%s,JSON_OBJECT('%s', {0})) = 0";


    @Override
    public void accept(QueryWrapper queryWrapper, DbField dbField) {
        String[] name = dbField.getName().split("\\.");
        //获取属性字段数据类型
        Class attrFieldClass = getAttrFieldClass(queryWrapper, name);
        String[] vals = splitStr(dbField.getValue());
        queryWrapper.nested(qw -> {
            QueryWrapper qw1 = (QueryWrapper) qw;
            for (int i = 0; i < vals.length; i++) {
                Object value = Convert.convert(attrFieldClass, vals[i]);
                qw1.apply(expressionSql(name[0], name[1]), value);
            }
        });
    }

    public static String sql(String colName, String attrName) {
        return JSON_NOT_CONTAINS_SQL_TEMP.formatted(colName, attrName);
    }

    @Override
    public String expressionSql(String colName, String attrName) {
        return sql(colName, attrName);
    }


    @Override
    public ComparatorEnum comparatorEnum() {
        return ComparatorEnum.JSON_OBJECT_NOT_CONTAINS;
    }
}
